package com.xscha.tableforge.constants;

import java.util.HashMap;
import java.util.Map;

/**
 * SQL类型常量
 */
public class SqlTypeConstants {

    private static final Map<String, String> SQL_TYPE_MAPPING = new HashMap<>();

    static {
        SQL_TYPE_MAPPING.put(FieldType.STRING.getCode(), "TEXT");
        SQL_TYPE_MAPPING.put(FieldType.INTEGER.getCode(), "INTEGER");
        SQL_TYPE_MAPPING.put(FieldType.DECIMAL.getCode(), "NUMERIC(19, 4)");
        SQL_TYPE_MAPPING.put(FieldType.BOOLEAN.getCode(), "BOOLEAN");
        SQL_TYPE_MAPPING.put(FieldType.DATE.getCode(), "DATE");
        SQL_TYPE_MAPPING.put(FieldType.DATETIME.getCode(), "TIMESTAMP WITH TIME ZONE");
        SQL_TYPE_MAPPING.put(FieldType.TIME.getCode(), "TIME");
        SQL_TYPE_MAPPING.put(FieldType.REFERENCE.getCode(), "VARCHAR(36)");
        SQL_TYPE_MAPPING.put(FieldType.NUMBER.getCode(), "NUMERIC(19, 4)");
        SQL_TYPE_MAPPING.put(FieldType.SINGLE_SELECT.getCode(), "TEXT");
        SQL_TYPE_MAPPING.put(FieldType.MULTI_SELECT.getCode(), "TEXT");
        SQL_TYPE_MAPPING.put(FieldType.TAGS.getCode(), "TEXT");
    }

    /**
     * 获取SQL类型
     * @param fieldType 字段类型
     * @param length 长度
     * @return SQL类型
     */
    public static String getSqlType(String fieldType, Integer length) {
        if (FieldType.STRING.getCode().equalsIgnoreCase(fieldType) && length != null && length > 0) {
            return "VARCHAR(" + length + ")";
        }
        return SQL_TYPE_MAPPING.getOrDefault(fieldType.toUpperCase(), "TEXT");
    }
}